
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>11.9. linecache — Random access to text lines &#8212; Python 3.6.3 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.6.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.6.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="11.10. shutil — High-level file operations" href="shutil.html" />
    <link rel="prev" title="11.8. fnmatch — Unix filename pattern matching" href="fnmatch.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/linecache.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
 

  </head>
  <body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="shutil.html" title="11.10. shutil — High-level file operations"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="fnmatch.html" title="11.8. fnmatch — Unix filename pattern matching"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">en</span>
          <span class="version_switcher_placeholder">3.6.3</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">11. File and Directory Access</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-linecache">
<span id="linecache-random-access-to-text-lines"></span><h1>11.9. <a class="reference internal" href="#module-linecache" title="linecache: This module provides random access to individual lines from text files."><code class="xref py py-mod docutils literal"><span class="pre">linecache</span></code></a> — Random access to text lines<a class="headerlink" href="#module-linecache" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/linecache.py">Lib/linecache.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-linecache" title="linecache: This module provides random access to individual lines from text files."><code class="xref py py-mod docutils literal"><span class="pre">linecache</span></code></a> module allows one to get any line from a Python source file, while
attempting to optimize internally, using a cache, the common case where many
lines are read from a single file.  This is used by the <a class="reference internal" href="traceback.html#module-traceback" title="traceback: Print or retrieve a stack traceback."><code class="xref py py-mod docutils literal"><span class="pre">traceback</span></code></a> module
to retrieve source lines for inclusion in  the formatted traceback.</p>
<p>The <a class="reference internal" href="tokenize.html#tokenize.open" title="tokenize.open"><code class="xref py py-func docutils literal"><span class="pre">tokenize.open()</span></code></a> function is used to open files. This
function uses <a class="reference internal" href="tokenize.html#tokenize.detect_encoding" title="tokenize.detect_encoding"><code class="xref py py-func docutils literal"><span class="pre">tokenize.detect_encoding()</span></code></a> to get the encoding of the
file; in the absence of an encoding token, the file encoding defaults to UTF-8.</p>
<p>The <a class="reference internal" href="#module-linecache" title="linecache: This module provides random access to individual lines from text files."><code class="xref py py-mod docutils literal"><span class="pre">linecache</span></code></a> module defines the following functions:</p>
<dl class="function">
<dt id="linecache.getline">
<code class="descclassname">linecache.</code><code class="descname">getline</code><span class="sig-paren">(</span><em>filename</em>, <em>lineno</em>, <em>module_globals=None</em><span class="sig-paren">)</span><a class="headerlink" href="#linecache.getline" title="Permalink to this definition">¶</a></dt>
<dd><p>Get line <em>lineno</em> from file named <em>filename</em>. This function will never raise an
exception — it will return <code class="docutils literal"><span class="pre">''</span></code> on errors (the terminating newline character
will be included for lines that are found).</p>
<p id="index-0">If a file named <em>filename</em> is not found, the function will look for it in the
module search path, <code class="docutils literal"><span class="pre">sys.path</span></code>, after first checking for a <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>
<code class="docutils literal"><span class="pre">__loader__</span></code> in <em>module_globals</em>, in case the module was imported from a
zipfile or other non-filesystem import source.</p>
</dd></dl>

<dl class="function">
<dt id="linecache.clearcache">
<code class="descclassname">linecache.</code><code class="descname">clearcache</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#linecache.clearcache" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the cache.  Use this function if you no longer need lines from files
previously read using <a class="reference internal" href="#linecache.getline" title="linecache.getline"><code class="xref py py-func docutils literal"><span class="pre">getline()</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="linecache.checkcache">
<code class="descclassname">linecache.</code><code class="descname">checkcache</code><span class="sig-paren">(</span><em>filename=None</em><span class="sig-paren">)</span><a class="headerlink" href="#linecache.checkcache" title="Permalink to this definition">¶</a></dt>
<dd><p>Check the cache for validity.  Use this function if files in the cache  may have
changed on disk, and you require the updated version.  If <em>filename</em> is omitted,
it will check all the entries in the cache.</p>
</dd></dl>

<dl class="function">
<dt id="linecache.lazycache">
<code class="descclassname">linecache.</code><code class="descname">lazycache</code><span class="sig-paren">(</span><em>filename</em>, <em>module_globals</em><span class="sig-paren">)</span><a class="headerlink" href="#linecache.lazycache" title="Permalink to this definition">¶</a></dt>
<dd><p>Capture enough detail about a non-file-based module to permit getting its
lines later via <a class="reference internal" href="#linecache.getline" title="linecache.getline"><code class="xref py py-func docutils literal"><span class="pre">getline()</span></code></a> even if <em>module_globals</em> is <code class="docutils literal"><span class="pre">None</span></code> in the later
call. This avoids doing I/O until a line is actually needed, without having
to carry the module globals around indefinitely.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.5.</span></p>
</div>
</dd></dl>

<p>Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">linecache</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">linecache</span><span class="o">.</span><span class="n">getline</span><span class="p">(</span><span class="n">linecache</span><span class="o">.</span><span class="vm">__file__</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="go">&#39;import sys\n&#39;</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="fnmatch.html"
                        title="previous chapter">11.8. <code class="docutils literal"><span class="pre">fnmatch</span></code> — Unix filename pattern matching</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="shutil.html"
                        title="next chapter">11.10. <code class="docutils literal"><span class="pre">shutil</span></code> — High-level file operations</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.6/Doc/library/linecache.rst"
            rel="nofollow">Show Source
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="shutil.html" title="11.10. shutil — High-level file operations"
             >next</a> |</li>
        <li class="right" >
          <a href="fnmatch.html" title="11.8. fnmatch — Unix filename pattern matching"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">en</span>
          <span class="version_switcher_placeholder">3.6.3</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="filesys.html" >11. File and Directory Access</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2017, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 14, 2017.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.4.
    </div>

  </body>
</html>